package cloud.tianai.mate.captcha.generator.impl;

import cloud.tianai.captcha.common.constant.CommonConstant;
import cloud.tianai.captcha.generator.ImageTransform;
import cloud.tianai.captcha.generator.common.model.dto.*;
import cloud.tianai.captcha.generator.common.util.CaptchaImageUtils;
import cloud.tianai.captcha.interceptor.CaptchaInterceptor;
import cloud.tianai.captcha.resource.ImageCaptchaResourceManager;
import cloud.tianai.captcha.resource.common.model.dto.Resource;
import lombok.AllArgsConstructor;
import lombok.NoArgsConstructor;

import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.nio.charset.StandardCharsets;
import java.util.List;
import java.util.*;
import java.util.concurrent.ThreadLocalRandom;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;

import static cloud.tianai.mate.captcha.common.constant.MateCaptchaTypeConstant.HONEYCOMB_IMAGE_CLICK;

/**
 * @Author: 天爱有情
 * @date 2023/11/8 10:44
 * @Description 蜂窝点选
 */
public class MateHoneycombClickImageGenerator extends AbstractClickImageCaptchaGenerator {

    private BufferedImage originClickImage;
    private Thread cleanThread;
    private long lastGenTime = 0L;
    private XY[] posArr = {
            new XY(7, 18),
            new XY(35, 4),
            new XY(63, 20),
            new XY(34, 34),
            new XY(63, 52),
            new XY(34, 66),
            new XY(6, 49),
    };

    protected Integer clickImgWidth = 100;
    protected Integer clickImgHeight = 100;

    public MateHoneycombClickImageGenerator(ImageCaptchaResourceManager imageCaptchaResourceManager, ImageTransform imageTransform, CaptchaInterceptor interceptor) {
        super(imageCaptchaResourceManager);
        setImageTransform(imageTransform);
        setInterceptor(interceptor);
    }

    public MateHoneycombClickImageGenerator() {
    }

    @Override
    protected List<ClickImageCheckDefinition> filterAndSortClickImageCheckDefinition(List<ClickImageCheckDefinition> allCheckDefinitionList) {
        Collections.shuffle(allCheckDefinitionList);
        return allCheckDefinitionList;
    }

    @Override
    protected List<Resource> randomGetClickImgTips(GenerateParam param) {
        // 随机生成1~7之间的数字，且不可重复，随机生成4个
//        Set<Integer> indexSet = new HashSet<>();
//        while (indexSet.size() != 4) {
//            int index = ThreadLocalRandom.current().nextInt(1, posArr.length + 1);
//            indexSet.add(index);
//        }
//        List<Resource> resources = new ArrayList<>();
//        for (Integer index : indexSet) {
//            resources.add(new CustomResource(null, String.valueOf(index)));
//        }

        Set<Resource> tipSet = new HashSet<>(4);
        while (tipSet.size() != 4) {
            Resource resource = getImageResourceManager().randomGetResource(CommonConstant.HONEYCOMB_CLICK_ICON, param.getTemplateImageTag());
            tipSet.add(resource);
        }

        return new ArrayList<>(tipSet);
    }

    @Override
    public ImgWrapper getClickImg(Resource tip, Color randomColor) {
        ThreadLocalRandom random = ThreadLocalRandom.current();
        int randomSize = ThreadLocalRandom.current().nextInt(1, posArr.length + 1);
        LinkedList<XY> indexs = new LinkedList<>(Arrays.asList(posArr));
        while (indexs.size() > randomSize) {
            int i = random.nextInt(0, indexs.size());
            indexs.remove(i);
        }
        // 随机出要填充的index位置后进行覆盖
        BufferedImage clickImage = CaptchaImageUtils.copyImage(getOriginClickImage(), BufferedImage.TYPE_INT_ARGB);
        BufferedImage mask = getResourceImage(tip);
        for (XY xy : indexs) {
            CaptchaImageUtils.overlayImage(clickImage, mask, xy.x, xy.y);
        }
        // 将该图片进行保存
        tip.setExtra(clickImage);

        // 再copy一份
        clickImage = CaptchaImageUtils.copyImage(clickImage, BufferedImage.TYPE_INT_ARGB);
        // 再来点随机旋转
        int randomDeg = randomInt(0, 85);
        clickImage = CaptchaImageUtils.rotateImage(clickImage, randomDeg);
        // 重置颜色，随机取颜色
        if (randomColor == null) {
            randomColor = CaptchaImageUtils.getRandomColor(random);
        }
//        Color randomColor = CaptchaImageUtils.getRandomColor(random);
        // 根据随机生成的颜色，将图片替换后生成后的颜色
        for (int y = 0; y < clickImage.getHeight(); y++) {
            for (int x = 0; x < clickImage.getWidth(); x++) {
                int rgb = clickImage.getRGB(x, y);
                int alpha = (rgb >> 24) & 0xff;
                // 透明度大于160才处理，过滤一下边缘过于透明的像素点
                if (alpha > 160) {
                    clickImage.setRGB(x, y, randomColor.getRGB());
                }
            }
        }
        return new ImgWrapper(clickImage, tip, null);
    }

    @Override
    protected void doInit() {
        // 初始化
//            ResourceStore resourceStore = imageCaptchaResourceManager.getResourceStore();
//            // 添加一些系统的资源文件
//            resourceStore.addResource(HONEYCOMB_IMAGE_CLICK, new Resource(ClassPathResourceProvider.NAME, DEFAULT_SLIDER_IMAGE_RESOURCE_PATH.concat("/1.jpg"), DEFAULT_TAG));
//
//
//            resourceStore.addResource(CommonConstant.HONEYCOMB_CLICK_ICON, new Resource(ClassPathResourceProvider.NAME, DEFAULT_SLIDER_IMAGE_RESOURCE_PATH.concat("/honeycomb/mask.png"), DEFAULT_TAG));
//            resourceStore.addResource(CommonConstant.HONEYCOMB_CLICK_ICON, new Resource(ClassPathResourceProvider.NAME, DEFAULT_SLIDER_IMAGE_RESOURCE_PATH.concat("/honeycomb/mask2.png"), DEFAULT_TAG));
//            resourceStore.addResource(CommonConstant.HONEYCOMB_CLICK_ICON, new Resource(ClassPathResourceProvider.NAME, DEFAULT_SLIDER_IMAGE_RESOURCE_PATH.concat("/honeycomb/mask3.png"), DEFAULT_TAG));
//            resourceStore.addResource(CommonConstant.HONEYCOMB_CLICK_ICON, new Resource(ClassPathResourceProvider.NAME, DEFAULT_SLIDER_IMAGE_RESOURCE_PATH.concat("/honeycomb/mask4.png"), DEFAULT_TAG));
//            resourceStore.addResource(CommonConstant.HONEYCOMB_CLICK_ICON, new Resource(ClassPathResourceProvider.NAME, DEFAULT_SLIDER_IMAGE_RESOURCE_PATH.concat("/honeycomb/mask5.png"), DEFAULT_TAG));
//            resourceStore.addResource(CommonConstant.HONEYCOMB_CLICK_ICON, new Resource(ClassPathResourceProvider.NAME, DEFAULT_SLIDER_IMAGE_RESOURCE_PATH.concat("/honeycomb/mask6.png"), DEFAULT_TAG));
//            resourceStore.addResource(CommonConstant.HONEYCOMB_CLICK_ICON, new Resource(ClassPathResourceProvider.NAME, DEFAULT_SLIDER_IMAGE_RESOURCE_PATH.concat("/honeycomb/mask7.png"), DEFAULT_TAG));
    }


    public BufferedImage getOriginClickImage() {
        if (originClickImage == null) {
            loadClickImageIfNecessary();
        }
        return originClickImage;
    }


    public synchronized void loadClickImageIfNecessary() {
        if (originClickImage == null) {
            String clickImageBase64 = "iVBORw0KGgoAAAANSUhEUgAAAGIAAABgCAYAAADmbacFAAATvUlEQVR4nO2dCbRV1XnH/+e++x7vgfgYn2AoOKAJIEkRBwoooihGMRgTI0klTepKk7ZJmrXaZlhdabuapGs1Kx2S1kxNqC0mMWmdMEEpCogjoKIChkkGARUlD5nfdO/t2q+/rZ/H++49595z7rvWfmud9cZzzt77m//ft/dVHdP7JN0vqZDwtU7SNfU27WwdjKEYtUm6VtJ7+WNOUhffBxGfUTBf3T2NzHeipPdLWitpf/pTiUb1yoipkj4oaaSkbknPSHpY0lFJLRGfERgGunvOlXSxpIGS5kraIOk/JB1LeS6RqB4Z8S5JV0o6m593SFok6Q5Jx1nIKOQZ0c083TNPkTRB0lhJ8yWtkvTr/ptq/ZIzH5+R9DwmpVPSdyWdmsCIHRP+WtIrPHufpD+SdNL/1cWslBoknS/pPhaqR9JD2PNMQu+YLGkJz8/hJ66u/VTrm4ZL+jtJB1moVyV9XtLQBEfdLOkPJO3iHc5//Ava8v+EPZ8t6SkWyNn1pZLOSXhx3HvOkPTPvCMvab2kT2AW3/F0pqTvEcE4RmyW9FFJA1JYGMeMOZLWwAinFb80oXK/UEMdSIAbw0ckfVLSCEknJN0pabGk11J65ytEUudJGkyY7ELjjURm70hyGfSdSKfThtU46LRNhQtj78FEufc+Iemy/mJAf2vEYJynS94GIZU/JGfoSPndJzBTE9FEl5/8RtKzbwetCGBelq+VXhmy3askPY1EuoX/RY1t9RhJ3yFfcWPYJunT/SGgcTPraZJ+G+npMnhOHPL3tEq6SNK7+dll0D+RtCWZqUWiF4nOnK/4HUnjAQRd/vJcDccRmTLAAreSaCWNiB7BJI3uh7kNk/Q5SS8zlt2S/oZsvp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dbFTWaw9l9Y9uPSH7SpkkUojqMEFxZoxOf65JG0+jsgb8Xwh8dkAI5R/yH5gNK2tl6MOydygSZoyBWm9DvFpO5p/G+GUANPqm8j9+9LcxImuQk9MvmHMF2PpAvScjdUxsHgh01Ge9na9ggUfc0FWjBQ+6PAzYm+ZkVjYCW/uN6/KdBnhXh3ncMNXMOlN815CT2H83JNUnQaeyM9dD7Nlp4+iuEr1saBx7jI5lNSHASFHC6zgsmQltkP8qmv6kePuHd0zEwrPdQHfRtLrtMkhf345sDTNIU9jlPI0Rdwx6QDTVoFYpE9aSWfkvTKrLuwUAfB4lsOitwql3mc7PnwJxXKGw9Uk8Zb73Zx4NsOp9MQaWNWsVEg97GoW6Y927wpII5Rq4/Ppz2bUVNdFnsMLF+UtdWWuLrzkHXk4/w5CTfmSFXS3Yd2/awrkoun6g5AM5pm9vY7krA9UOS/gfB98cgmQAoEAAAAABJRU5ErkJggg==";
            byte[] imageBytes = Base64.getDecoder().decode(clickImageBase64.getBytes(StandardCharsets.UTF_8));
            this.originClickImage = CaptchaImageUtils.wrapFile2BufferedImage(new ByteArrayInputStream(imageBytes));
            lastGenTime = System.currentTimeMillis();
            if (cleanThread == null) {
                // 开启一个清理线程
                cleanThread = new Thread(() -> {
                    // 定时清理缓存图片
                    while (true) {
                        try {
                            TimeUnit.HOURS.sleep(1);
                            if (originClickImage != null) {
                                if (System.currentTimeMillis() - lastGenTime > 3600000) {
                                    // 超过1小时，清理一下
                                    this.originClickImage = null;
                                }
                            }
                        } catch (Exception e) {
                            // ignore
                        }
                    }
                });
                cleanThread.setDaemon(true);
                cleanThread.start();
            }
        }
    }


    @Override
    protected ImageCaptchaInfo doWrapImageCaptchaInfo(CaptchaExchange captchaExchange) {
        List<ClickImageCheckDefinition> checkClickImageCheckDefinitionList = (List<ClickImageCheckDefinition>) captchaExchange.getTransferData();
        GenerateParam param = captchaExchange.getParam();
        BufferedImage bgImage = captchaExchange.getBackgroundImage();
        Resource resourceImage = captchaExchange.getResourceImage();
        CustomData customData = captchaExchange.getCustomData();
        // 提示图片
        BufferedImage tipImage = genTipImage(checkClickImageCheckDefinitionList).getImage();
        ImageTransformData transform = getImageTransform().transform(param, bgImage, tipImage, resourceImage, checkClickImageCheckDefinitionList, customData);
        ImageCaptchaInfo clickImageCaptchaInfo = new ImageCaptchaInfo();
        clickImageCaptchaInfo.setBackgroundImage(transform.getBackgroundImageUrl());
        clickImageCaptchaInfo.setBackgroundImageTag(resourceImage.getTag());
        clickImageCaptchaInfo.setTemplateImage(transform.getTemplateImageUrl());
        clickImageCaptchaInfo.setBackgroundImageWidth(bgImage.getWidth());
        clickImageCaptchaInfo.setBackgroundImageHeight(bgImage.getHeight());
        clickImageCaptchaInfo.setTemplateImageWidth(tipImage.getWidth());
        clickImageCaptchaInfo.setTemplateImageHeight(tipImage.getHeight());
        clickImageCaptchaInfo.setRandomX(null);
        clickImageCaptchaInfo.setTolerant(null);
        // 都属于图片点选
        clickImageCaptchaInfo.setType(HONEYCOMB_IMAGE_CLICK);
        customData.setExpand(checkClickImageCheckDefinitionList);
        return clickImageCaptchaInfo;
    }

    private ImgWrapper genTipImage(List<ClickImageCheckDefinition> imageCheckDefinitions) {
        BufferedImage tipImage = new BufferedImage(clickImgWidth * imageCheckDefinitions.size(), clickImgHeight, BufferedImage.TYPE_INT_ARGB);
        Graphics2D graphics = tipImage.createGraphics();
        String tips = imageCheckDefinitions.stream().map(c -> c.getTip().getData()).collect(Collectors.joining());

        for (int i = 0; i < imageCheckDefinitions.size(); i++) {
            ClickImageCheckDefinition imageCheckDefinition = imageCheckDefinitions.get(i);
            Resource tip = imageCheckDefinition.getTip();
            graphics.drawImage((Image) tip.getExtra(), i * clickImgWidth, 0, clickImgWidth, clickImgHeight, null);
            // 使用完后销毁
            tip.setExtra(null);
        }
        graphics.dispose();
//        tipImage = CaptchaImageUtils.gray(tipImage);
        return new ImgWrapper(tipImage, new Resource(null, tips), null);
    }


    @NoArgsConstructor
    @AllArgsConstructor
    public static class XY {
        int x;
        int y;

    }

}
